﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;

namespace MyControl {

  #region 树型结构
  /// <summary>
  /// 树型结构
  /// </summary>
  #endregion
  public class Tree : INotifyPropertyChanged {

	#region 所属元素
	/// <summary>
	/// 所属元素
	/// </summary>
	#endregion
	public object Origin { get; set; } = null;

	#region 父节点
	/// <summary>
	/// 父节点
	/// </summary>
	#endregion
	public Tree Parent { get; set; } = null;

	#region 标签名称
	/// <summary>
	/// 标签名称
	/// </summary>
	#endregion
	public string Name { get; set; } = null;

	#region 内容
	/// <summary>
	/// 内容
	/// </summary>
	#endregion
	public object Value { get; set; } = null;

	#region 鼠标正悬停节点
	/// <summary>
	/// 鼠标正悬停节点
	/// </summary>
	#endregion
	public bool IsHover {
	  get {
		return this.isHover;
	  }
	  set {
		this.isHover = value;
		this.TriggerPropertyChanged("IsHover");
	  }
	}
	private bool isHover = false;

	#region 当前节点已选择
	/// <summary>
	/// 当前节点已选择
	/// </summary>
	#endregion
	public bool IsSelect {
	  get {
		return this.isSelect;
	  }
	  set {
		this.isSelect = value;
		this.TriggerPropertyChanged("IsSelect");
	  }
	}
	private bool isSelect = false;

	#region 子级元素
	/// <summary>
	/// 子级元素
	/// </summary>
	#endregion
	public List<Tree> Children {
	  get {
		return this.children;
	  }
	  set {
		this.children = value;
		foreach (var tree in value) {
		  tree.Parent = this;
		}
	  }
	}
	private List<Tree> children = null;

	#region 是否包含子节点
	/// <summary>
	/// 是否包含子节点
	/// </summary>
	#endregion
	public bool HasChildren {
	  get {
		return this.Children != null && this.Children.Count > 0;
	  }
	}

	#region 是否显示图标
	/// <summary>
	/// 是否显示图标
	/// </summary>
	#endregion
	public Visibility RightVisibility {
	  get {
		return this.HasChildren ? Visibility.Visible : Visibility.Hidden;
	  }
	}

	#region 创建树形结构
	/// <summary>
	/// 创建树形结构
	/// </summary>
	#endregion
	public Tree() {
	}

	#region 创建树形结构
	/// <summary>
	/// 创建树形结构
	/// </summary>
	/// <param name="name">标签名称</param>
	#endregion
	public Tree(string name) {
	  this.Name = name;
	  this.Value = null;
	}

	#region 创建树形结构
	/// <summary>
	/// 创建树形结构
	/// </summary>
	/// <param name="name">标签名称</param>
	/// <param name="value">标签内容</param>
	#endregion
	public Tree(string name, object value) {
	  this.Name = name;
	  this.Value = value;
	}

	#region 创建树形结构
	/// <summary>
	/// 创建树形结构
	/// </summary>
	/// <param name="name">标签名称</param>
	/// <param name="children">子节点列表</param>
	#endregion
	public Tree(string name, List<Tree> children) {
	  this.Name = name;
	  this.Children = children;
	}

	#region 创建树形结构
	/// <summary>
	/// 创建树形结构
	/// </summary>
	/// <param name="name">标签名称</param>
	/// <param name="value">标签内容</param>
	/// <param name="children">子节点列表</param>
	#endregion
	public Tree(string name, object value, List<Tree> children) {
	  this.Name = name;
	  this.Value = value;
	  this.Children = children;
	}

	#region 添加子节点
	/// <summary>
	/// 添加子节点
	/// </summary>
	/// <param name="tree">要添加的子节点</param>
	#endregion
	public void AppendChild(Tree tree) {
	  tree.Parent = this;
	  if (this.Children == null) {
		this.Children = new List<Tree>();
	  }
	  this.Children.Add(tree);
	}

	#region 属性变化事件
	/// <summary>
	/// 属性变化事件
	/// </summary>
	#endregion
	public event PropertyChangedEventHandler PropertyChanged;

	#region 触发属性变化
	/// <summary>
	/// 触发属性变化
	/// </summary>
	/// <param name="propertyName">触发属性名称</param>
	#endregion
	private void TriggerPropertyChanged(string propertyName) {
	  if (PropertyChanged != null) {
		PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
	  }
	}
  }
}